<?php
/**
*
* @authors 丛玉坤(you@example.org)
* @date    2016-06-25 22:00:22
* @version 一版
*/
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
		public function index()
		{
				if($_POST){
				$tel=$_POST['mobile'];
				$User=D("User");
				$uarr=$User->field('tel')->where('tel='.$tel)->find();
				    if ($uarr['tel']!='') {
				          echo "<script>
				                  alert('该电话已注册，请更换后重试');
				                  history.go(-1);
				                </script>";
				                die;
				    }

				  // echo '<pre>';print_r($_POST);print_r($_SESSION);
				  if($_POST['mobile']!=$_SESSION['mobile'] or $_POST['mobile_code']!=$_SESSION['mobile_code'] or empty($_POST['mobile']) or empty($_POST['mobile_code'])){
				    exit('手机验证码输入错误。'); 
				  }else{

				    $_SESSION['mobile'] = '';
				    $_SESSION['mobile_code'] = '';  
				            $data['tel']=I('post.mobile');
				            $data['password']=md5(I("pwd"));
				            $data['username']='天天互保'.GetRandStr(4);
				            $data['tsrc']="/Public/Home/images/defaultimg.png";
				            if (!empty($data)) {
				              $u=$User->add($data);
				               $Qian=M("Qianbao");
								 $Ua['uid']=$u;
								 $q=$Qian->add($Ua);
				              if ($u&&$q) {
				              		$_SESSION['id']=$u;
				                    echo "<script>
				                            alert('注册成功');
				                            location.href='/Member/index';
				                          </script>";
				                	die;
				              }else{
				                $u->getError();
				              }
				            }
				       
				    
				  }
				}
			$_SESSION['send_code'] = random(6,1);
	         $this->display();
	      }
	  
 //短信
 public function sms()
{
  header("Content-type:text/html; charset=UTF-8");

$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

$mobile = $_POST['mobile'];
$send_code = $_POST['send_code'];

$mobile_code = random(4,1);
if(empty($mobile)){
  exit('手机号码不能为空');
}

if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){
  //防用户恶意请求
  exit('请求超时，请刷新页面后重试');
}

$post_data = "account=cf_yingteng&password=1234567&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
//密码可以使用明文密码或使用32位MD5加密
$gets =  xml_to_array(Post($post_data, $target));
if($gets['SubmitResult']['code']==2){
  $_SESSION['mobile'] = $mobile;
  $_SESSION['mobile_code'] = $mobile_code;
}
echo $gets['SubmitResult']['msg'];
}

	
public function login(){  
        // //判断是否永久登录
        $this->checkLong();
        //已经登录则跳转至个人中心
        if(isset($_SESSION['username'])){
            $this->redirect('/Member');
        }else{ 
            //判断是否存在cookie    
            if(isset($_COOKIE['username'])){
                $this->assign('username',$_COOKIE['username']);
                $this->pass=base64_decode(base64_decode($_COOKIE['password']));
            }
        //     //显示注册页
            $this->display();
        }
    }

    //显示验证码
    public function verifyImg(){
    ob_clean();
    $Verify =     new \Think\Verify();// 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片
    $Verify->useImgBg = true; 
    $Verify->length   = 4;
    $Verify->entry();
    }

    //验证登录
    public function yanzheng(){
        $verify = new \Think\Verify();
        if($verify->check(I("yzm"))){
            //判断用户名密码
            $user = new \Home\Model\UserModel();
            $res = $user->checkName(I("username"),I("pwd"));
            if($res === false){ 
                    echo "<script>
                            alert('用户名或密码错误');
                            history.go(-1);
                          </script>";
            }else{
                //用户信息存入session
                session("username",$res['tel']);
                session("id",$res['id']);
                //如果用户勾选了"记住我",则保持持久登陆
                if(I("remember")){
                    $salt = $this->random_str(16);
                    //第二分身标识
                    $identifier = md5($salt . md5(I("username") . $salt));
                    //永久登录标识
                    $token = md5(uniqid(rand(), true));
                    //永久登录超时时间(1周)
                    $timeout = time()+3600*24*7;

                    //存入cookie
                    cookie('auth',"$identifier:$token",$timeout);
                    $user->saveRemember($res['id'],$identifier,$token,$timeout);
                }

                //把用户名存入cookie，退出登录后在表单保存用户名信息
                // setcookie('username',I('username'),time()+3600*24);
					cookie('username',I('username'),3600*24);
				    cookie("password",base64_encode(base64_encode(I("pwd"))),3600*24);
                //跳转至会员中心
                $this->redirect('/Member');
            }
        }else{
					echo "<script>
                            alert('验证码输入输入错误');
                            history.go(-1);
                          </script>";
        }
    }    

    //测试strstr函数
    public function strstrtest(){
        $param = "Think\Verify";
        //第三个参数为true，返回'Think'；没有第三个参数，返回'\Verify'
        $name = strstr($param,'\\',true);
        echo $name;
    }

    //用户中心
    public function ucenter(){
        //判断是否永久登录
        $this->checkLong();

        $this->assign("session",$_SESSION);
        $this->display("ucenter");
    }

    //退出登录
    public function loginout(){
        session(null);
        setcookie('auth', '', time()-1);
        $this->redirect("/login/login");
    }

    //生成随机数,用于生成salt
    public function random_str($length){
        //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
        $str = '';
        $arr_len = count($arr);
        for ($i = 0; $i < $length; $i++){
            $rand = mt_rand(0, $arr_len-1);
            $str.=$arr[$rand];
        }
        return $str;
    }

    //判断是否持久登录
    public function checkLong(){
  
   

        $check = new \Home\Model\UserModel();
        $is_long = $check->checkRemember();
        if($is_long === false){
            
        }else{
            session("username",$is_long['tel']);
            session("id",$is_long['id']);
        }
    }



}
